package project.dade.cn.bst.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.MenuItem;

import com.alibaba.fastjson.JSON;
import com.squareup.okhttp.Request;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.InjectView;
import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;
import project.dade.cn.bst.R;
import project.dade.cn.bst.adapter.base.ProductAadapter;
import project.dade.cn.bst.base.ZJPBaseBarActivity;
import project.dade.cn.bst.manager.ApiConfig;
import project.dade.cn.bst.model.BaseRequestsModel;
import project.dade.cn.bst.model.CommentModel;
import project.dade.cn.bst.model.ProdectCommentModel;
import project.dade.cn.bst.network.FormatRequest;
import project.dade.cn.bst.network.callback.ResultCallback;
import project.dade.cn.bst.network.request.OkHttpRequest;
import project.dade.cn.bst.utils.AppUtil;
import project.dade.cn.bst.utils.LogUtil;
import project.dade.cn.bst.utils.NetRequestUtil;
import project.dade.cn.bst.utils.ToastUtil;
import project.dade.cn.bst.views.EmptyLayout;

/**
 * Created by vain zhou on 2016/5/5.
 */
public class ProductCommentActivity extends ZJPBaseBarActivity implements BGARefreshLayout.BGARefreshLayoutDelegate  {

    private ProductAadapter productAadapter;
    @InjectView(R.id.rl_recyclerview_refresh)
    BGARefreshLayout mRefreshLayout;
    @InjectView(R.id.error_layout)
    EmptyLayout mErrorLayout;
    @InjectView(R.id.rv_recyclerview_data)
    RecyclerView recyclerView;
    private String id;
    private int page = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.acticity_prodectcomment);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        productAadapter = new ProductAadapter( recyclerView);
        id=getIntent().getStringExtra("id");
        recyclerView.setAdapter(productAadapter);    mRefreshLayout.setDelegate(this);
        mRefreshLayout.setRefreshViewHolder(new BGANormalRefreshViewHolder(ProductCommentActivity.this, true));

    }

    @Override

    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        if (AppUtil.isNetworkAvailable(this)) {
            page=1;
           requestGetComment();
        } else {
            mErrorLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
        }

    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        if (AppUtil.isNetworkAvailable(this)) {
            page++;

            requestMoreComment();
        } else {
            mRefreshLayout.endLoadingMore();
            Snackbar.make(recyclerView, R.string.tips_network_error, Snackbar.LENGTH_SHORT).show();
        }
        return true;

    }

    private void requestMoreComment() {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("proid", id);
        params.put("Sign", FormatRequest.main(params));
        new OkHttpRequest.Builder()
                .url(ApiConfig.getHostAddress() + ApiConfig.SERVER_URL.Commentlist).params(params)
                .post(new ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Exception e) {
                        mRefreshLayout.endRefreshing();
                        ToastUtil.showShort(ProductCommentActivity.this, "网络相应失败请稍后再试");
                    }

                    @Override
                    public void onResponse(String response) {
                        mRefreshLayout.endRefreshing();
                        LogUtil.i(TAG, "获取评论列表：" + response);
                        if (TextUtils.isEmpty(NetRequestUtil.requestsError(response, "获取评论列表"))) {
                            BaseRequestsModel model = JSON.parseObject(response, BaseRequestsModel.class);
                            //获取请求成功
                            List<ProdectCommentModel> prodectCommentModels = new ArrayList<>();
                            prodectCommentModels = JSON.parseArray(model.getData(), ProdectCommentModel.class);
                            if (prodectCommentModels.size()==0) {
                                ToastUtil.showShort(ProductCommentActivity.this, "没有更多的数据了");
                                page--;
                            }else{
                                 productAadapter.addMoreDatas(prodectCommentModels);
                                }

                        } else {
                            mRefreshLayout.endLoadingMore();
                            ToastUtil.showShort(ProductCommentActivity.this, NetRequestUtil.requestsError(response, "获取订单列表"));
                            mErrorLayout.setErrorType(EmptyLayout.NODATA);
                            mErrorLayout.setErrorMessage("数据请求失误");

                        }
                    }

                });


    }

    @Override
    protected void onResume() {
        super.onResume();
        initRequest();
    }

    private void initRequest() {
        if (AppUtil.isNetworkAvailable(this)) {
            requestGetComment();
        } else {
            mErrorLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
        }

    }

    private void requestGetComment() {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("proid", id);
        params.put("Sign", FormatRequest.main(params));
        new OkHttpRequest.Builder()
                .url(ApiConfig.getHostAddress() + ApiConfig.SERVER_URL.Commentlist).params(params)
                .post(new ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Exception e) {
                        mRefreshLayout.endRefreshing();
                        ToastUtil.showShort(ProductCommentActivity.this, "网络相应失败请稍后再试");
                    }

                    @Override
                    public void onResponse(String response) {
                        mRefreshLayout.endRefreshing();
                        LogUtil.i(TAG, "获取评论列表：" + response);
                        if (TextUtils.isEmpty(NetRequestUtil.requestsError(response, "获取评论列表"))) {
                            BaseRequestsModel model = JSON.parseObject(response, BaseRequestsModel.class);
                            LogUtil.i(TAG, "获取收藏商品：" + model.getData());

                            //获取请求成功
                            List<ProdectCommentModel> prodectCommentModels = new ArrayList<>();
                           prodectCommentModels = JSON.parseArray(model.getData(), ProdectCommentModel.class);
                            productAadapter.setDatas(prodectCommentModels);

                            if (prodectCommentModels.size()==0) {
                                mErrorLayout.setErrorType(EmptyLayout.NODATA);
                                mErrorLayout.setErrorMessage("暂无内容");
                            }

                        } else {
                            mRefreshLayout.endRefreshing();
                            ToastUtil.showShort(ProductCommentActivity.this, NetRequestUtil.requestsError(response, "获取订单列表"));
                            mErrorLayout.setErrorType(EmptyLayout.NODATA);
                            mErrorLayout.setErrorMessage("数据请求失误");

                        }
                    }

                });

    }
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            this.finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
